package com.masterclient.app.businessmodule.usermodule.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.router.map.FCRouterPath;
import com.masterclient.app.R;
import com.eoner.baselib.utils.notify.RxBus;
import com.eoner.baselibrary.bean.address.AddressListBean;
import com.eoner.baselibrary.bean.personal.UploadImgBean;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.usermodule.userinfo.contract.UserInfoContract;
import com.masterclient.app.businessmodule.usermodule.userinfo.contract.UserInfoPresenter;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/8/3 13:34
 * 功能说明：
 */
@Route(path = FCRouterPath.USER_MODIFY_NICK_NAME)
public class UpdateNameActivity extends FCBusinessActivity<UserInfoPresenter> implements UserInfoContract.View {

    @BindView(R.id.et_nick_name)
    EditText etNickName;

    public static final int RESULRCODE = 1111;

    RxBus rxBus;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_update_username;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setTitle("个人信息").setRightBtnText("提交");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        if(TextUtils.isEmpty(UserDataManager.getInstance().getNickName())){
            etNickName.setText("");
        }else {
            etNickName.setText(UserDataManager.getInstance().getNickName());
            etNickName.setSelection(UserDataManager.getInstance().getNickName().length() > 15 ? 15 : UserDataManager.getInstance().getNickName().length());
        }
        etNickName.setFilters(new InputFilter[]{new MyInputFilter(),new MyInputFilter2(),new InputFilter.LengthFilter(15)});
        rxBus = RxBus.$();
    }

    @Override
    protected void onNavigationRightClick() {
        super.onNavigationRightClick();
        String nickName = etNickName.getText().toString().trim();
        if(nickName.length()>=2){
            if(nickName.length()>15){
                showToast("昵称不能多于15个字符");
            }else {

                Map<String, String> map = new HashMap<>();
                map.put("nick_name", nickName);
                mPresenter.updateUserInfo(map);
            }
        }else {
            showToast("昵称不能少于两个字符");
        }
    }

    @Override
    public void onUploadSuccess(UploadImgBean.DataBean dataBean) {

    }

    @Override
    public void onUpdateHeadImgSucccess() {

        rxBus.post("nikeName","nikeName");
        Intent intent = new Intent();
        intent.putExtra("nickname",etNickName.getText().toString().trim());
        setResult(RESULRCODE,intent);
        finish();
    }

    @Override
    public void getAddressList(AddressListBean.DataBean dataBean) {

    }

    @Override
    public void mobile_change_state_result(String code, String msg) {

    }

    class MyInputFilter implements InputFilter{

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            if(source.equals(" ")){
                return "";
            }else{
                return null;
            }
        }
    }

    class MyInputFilter2 implements InputFilter{

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“'。，、？]";
            Pattern pattern = Pattern.compile(speChat);
            Matcher matcher = pattern.matcher(source.toString());
            if(matcher.find())return "";
            else return null;
        }
    }

    @Override
    protected void onPause() {
        hideSoftInput(etNickName);
        super.onPause();
    }
}
